package MyApp::Workflow::Action::AgreeAccount;

use strict;
use base qw( Workflow::Action );
use Workflow::Exception qw( persist_error );
use Workflow::Factory qw( FACTORY );

sub execute {
    my ($self,$wf) = @_;
    my $db=$wf->context->param('db');

    my $issue_id = $db->query('select number from daiban where workflow_id=? AND workflow_type=2',
                              $wf->id)->array->[0];
    my $account_hashes = $db->query('select * from account_application where issue_id=?',
                                     $issue_id)->hashes;
    
    foreach my $hash (@$account_hashes) {
        if ($hash->{operation} == 1) {
            my $company_name = $db->query('select name from companylist where id=?',$hash->{company_id})->array->[0];
            $db->query('insert into vpn_account (username,company_id,password,tibaoren,issue_id) values (?,?,?,?,?)',
                        $hash->{username},$hash->{company_id},$hash->{password},$wf->context->param('creator'),$hash->{issue_id});
            $db->query('insert into account_dealing (username,company_name,password,operation) values (?,?,?,1)',
                        $hash->{username},$company_name,$hash->{password});
        } else {

            my $company_name = $db->query('select name from companylist where id=?',$hash->{company_id})->array->[0];

            #当账户删除时，账户处理列表中插入数据（处理完成后再删除台账表中数据）
            $db->query('insert into account_dealing (username,company_name,password,operation) values (?,?,?,?)',
                        $hash->{username},$company_name,$hash->{password},$hash->{operation});
         
        }

        $db->query('update daiban set is_finish=1 where workflow_type=2 and workflow_id=?',$wf->id);
    }

    $wf->add_history(
        Workflow::History->new({
            action      => '审核',
            description => sprintf("同意!"),
            user        => $wf->context->param('current_user'),
            time_zone   => 'local'
        })
    );
};

1;
